<!--
  ~ Copyright (c) 2020 the original author or authors
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      https://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  ~ or implied. See the License for the specific language governing
  ~ permissions and limitations under the License.
  -->

<div class="container">
  <h1 class="container--title">{{ 'common.capital.edit' | translate }} {{ data.type }}</h1>
  <form #form="ngForm">
    <div class="form-field">
      <label>{{ 'common.name' | translate }}</label>
      <input
        [(ngModel)]="initialName"
        (input)="onChange(initialName)"
        [ngModelOptions]="{ standalone: true }"
        placeholder="{{ data.type }} {{ 'common.name' | translate }}"
      />
      <div class="err">
        <ng-container *ngIf="alreadyExists">
          {{ data.errorMsg }}
        </ng-container>
        <ng-container *ngIf="initialName.length > maxInputLength && !isForbiddenChar">
          {{ 'common.input_lengh_err' | translate }}
        </ng-container>
        <ng-container *ngIf="isForbiddenChar">
          {{ 'common.forbiden_chars_err' | translate }}
        </ng-container>
      </div>
    </div>
    <div class="content">
      <p class="label">{{ data.label }}</p>
      <p *ngIf="data.entityType; else ownerName" class="type">{{ data.entityType | lowercase }}</p>
      <ng-template #ownerName>
        <p class="owner">{{ data.ownerName }}</p>
      </ng-template>
    </div>
    <div class="action-btns">
      <button class="cancel-btn" type="button" mat-dialog-close>
        {{ 'common.capital.cancel' | translate | titlecase }}
      </button>

      <button
        type="submit"
        class="save-btn"
        [disabled]="isRenameDisabled || !initialName || alreadyExists || initialName.length > maxInputLength || isForbiddenChar"
        (click)="onSave()"
      >
        {{ 'common.capital.save' | translate }}
      </button>
    </div>
  </form>

  <div class="delete-container" mat-dialog-actions>
    <mat-accordion>
      <mat-expansion-panel hideToggle (opened)="panelOpenState = true">
        <mat-expansion-panel-header>
          <mat-icon svgIcon="alert"></mat-icon>
          {{ 'common.capital.delete' | translate }} {{ data.type }}
        </mat-expansion-panel-header>

        <div *ngIf="panelOpenState">
          <span class="confirmation--question">
            {{ 'models.delete_dialog.confirmation_question' | translate : { entityType: data.type | lowercase } }}
          </span>
          <div class="form-field">
            <input [(ngModel)]="deleteInput" placeholder="{{ data.type }} {{ 'common.name' | translate }}" />
          </div>
          <button class="del-btn" (click)="onDelete()" [disabled]="data.entityName !== deleteInput">
            {{ 'common.capital.delete' | translate }} {{ data.type }}
          </button>
        </div>
      </mat-expansion-panel>
    </mat-accordion>
  </div>

  <button class="button-cross" mat-icon-button [mat-dialog-close]="false">
    <mat-icon svgIcon="cross_new" inline="true"></mat-icon>
  </button>
</div>
